package com.ruoyi.finance.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.finance.domain.AccountSet;
import com.ruoyi.finance.service.IAccountSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 账套管理Controller
 *
 * @author ruoyi
 * @date 2025-02-24
 */
@RestController
@RequestMapping("/finance_base/account")
public class AccountSetController extends BaseController
{
    @Autowired
    private IAccountSetService accountSetService;

    /**
     * 查询账套管理列表
     */
    @PreAuthorize("@ss.hasPermi('finance_base:account:list')")
    @GetMapping("/list")
    public TableDataInfo list(AccountSet accountSet)
    {
        startPage();
        List<AccountSet> list = accountSetService.selectAccountSetList(accountSet);
        return getDataTable(list);
    }


    /**
     * 查询账套列表 -查询每个用户的默认账套
     */
//    @PreAuthorize("@ss.hasPermi('finance_base:account:list')")
//    @GetMapping("/listAccountSetAll")
//    public AjaxResult listAccountSetAll()
//    {
//        List<AccountSet> list = accountSetService.listAccountSetAllByUserId();
//        return  success(list);
//    }

    /**
     * 查询账套列表 -查询每个用户的默认账套
     * @return
     */
    @PreAuthorize("@ss.hasPermi('finance_base:account:list')")
    @GetMapping("/listAccountSetAll")
    public AjaxResult listAccountSetAll()
    {
        List<AccountSet> list = accountSetService.listAccountSetAllByUserId();
        return  success(list);
    }


    /**
     * 用户切换默认账套
     * @param accountId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('finance_base:account:edit')")
    @PutMapping("/checkDefaultAccountSet/{accountId}")
    public AjaxResult checkDefaultAccountSet(@PathVariable("accountId") Long accountId){
        if (accountId == null){
            return AjaxResult.error("参数错误");
        }

         return toAjax(accountSetService.checkDefaultAccountSet(accountId));
    }











    /**
     * 导出账套管理列表
     */
    @PreAuthorize("@ss.hasPermi('finance_base:account:export')")
    @Log(title = "账套管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccountSet accountSet)
    {
        List<AccountSet> list = accountSetService.selectAccountSetList(accountSet);
        ExcelUtil<AccountSet> util = new ExcelUtil<AccountSet>(AccountSet.class);
        util.exportExcel(response, list, "账套管理数据");
    }





    /**
     * 获取账套管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('finance_base:account:query')")
    @GetMapping(value = "/{accountId}")
    public AjaxResult getInfo(@PathVariable("accountId") Long accountId)
    {
        return success(accountSetService.selectAccountSetByAccountId(accountId));
    }

    /**
     * 新增账套管理
     */
    @PreAuthorize("@ss.hasPermi('finance_base:account:add')")
    @Log(title = "账套管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AccountSet accountSet)
    {
        return toAjax(accountSetService.insertAccountSet(accountSet));
    }

    /**
     * 修改账套管理
     */
    @PreAuthorize("@ss.hasPermi('finance_base:account:edit')")
    @Log(title = "账套管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AccountSet accountSet)
    {
        return toAjax(accountSetService.updateAccountSet(accountSet));
    }

    /**
     * 删除账套管理
     */
    @PreAuthorize("@ss.hasPermi('finance_base:account:remove')")
    @Log(title = "账套管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{accountIds}")
    public AjaxResult remove(@PathVariable Long[] accountIds)
    {
        return toAjax(accountSetService.deleteAccountSetByAccountIds(accountIds));
    }
}
